﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Orm.Model;

namespace OpenSoftStore.Client.UIL.ManageMember
{
    public partial class PreorderAddWindow : Form
    {
        private const string _ShopId = "b0def1a7-bf25-45f2-b65a-6dc586dbfc2c";
        private AdminLogic adminBll;
        private MemberLogic memberBll;
        private T_Preorder preorder;

        public PreorderAddWindow(string memberId)
        {
            InitializeComponent();

            adminBll = new AdminLogic();
            memberBll = new MemberLogic();
            preorder = new T_Preorder();


            cmbAdmin.DisplayMember = "Name";
            cmbAdmin.ValueMember = "Id";
            cmbAdmin.DataSource = adminBll.GetAdminList();

            if (memberId != null)
            {
                preorder.MemberId = memberId;
                txtCard.Text = memberBll.Where<T_Member>(m => m.Id == memberId).FirstOrDefault().CardId;
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        public void GetPreorder(string id)
        {
            preorder.Id = id;
            var table = memberBll.GetPreorderTable(" where P.Id='" + id + "'");
            DataRow row = table.Rows[0];
            txtCard.Text = Convert.ToString(row["CardId"]);
            txtContent.Text = Convert.ToString(row["Remark"]);
            cmbAdmin.Text = Convert.ToString(row["AdminName"]);
            DateTime addTime = Convert.ToDateTime(row["AddTime"]);
            dtpDate.Value = addTime;
            dtpTime.Value = addTime;
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (preorder.Id == null)
            {
                preorder.Id = Guid.NewGuid().ToString("D");
                preorder.Remark = txtContent.Text;
                preorder.AdminId = cmbAdmin.SelectedValue.ToString();
                preorder.AddTime = dtpDate.Value.Date.Add(dtpTime.Value.TimeOfDay);
                preorder.ShopId = _ShopId;
                memberBll.AddPreorder(preorder);
            }
            else
            {
                preorder.Remark = txtContent.Text;
                preorder.AdminId = cmbAdmin.SelectedValue.ToString();
                preorder.AddTime = dtpDate.Value.Date.Add(dtpTime.Value.TimeOfDay);
                memberBll.Update(preorder);
            }

            MemberList memberList = new MemberList();
            memberList.btnQuery_Click(null, null);
            this.Close();
        }
    }
}
